var ROOT="D:/DO_AN/OneDrive/do.an/source/public/nodejs";
var PATH_MODULE=ROOT+"/node_modules/";
var PATH="D:/DO_AN/OneDrive/do.an/source/public/nodejs/log_service_amobi";
var path = require('path');
var fs = require('fs');
require(ROOT+"/datejs.js");
var finish=0;
var STEP_MINUTE=60;
var STRING_FOLDER="yyyy-MM-dd HH";

var InfiniteLoop = require(PATH_MODULE+'infinite-loop');

var il = new InfiniteLoop();
il.add(main).run();

var STEP=require(PATH_MODULE+"step-master");

function main(){
	var MongoClient = require(PATH_MODULE+'mongodb').MongoClient;
	STEP(
	function step1(){
	MongoClient.connect('mongodb://127.0.0.1:27017/amobi', function(err, db) {
//	while(true){
			//đọc file start.txt xác định thời gian cuối cùng cập nhật data(NULL đọc toàn bộ từ trước tới thời điểm hiện tại)
			var start=fs.readFileSync(ROOT+"/start.txt","utf8");
			start=start.split(" ");
			var date_start=new Date(start[0]);
			date_start.set({
				hour:parseInt(start[1]),
				minute:parseInt(start[2]),
			});
			//khong lay duoc ngay bat dau dat ngay mac dinh.
			if(!date_start.getMonth()&&date_start.getMonth()!=0){
				var date_start=new Date();
				date_start.set({
					year:2014,
					month:0,
					day:1,
					hour:0,
					minute:STEP_MINUTE
				})
			}
			var date_end=new Date();
			date_end=date_end.addMinutes(-1*STEP_MINUTE);
			if(date_start.toString(STRING_FOLDER)<=date_end.toString(STRING_FOLDER)){
					console.log(date_start);
				//Gio nho hon doc cap nhat vao db;
				for(;date_start.toString(STRING_FOLDER)<=date_end.toString(STRING_FOLDER);){
					
					//LOG ADV VIEWS
					var path_adv_view=PATH+"/adv/views/"+date_start.toString("yyyy-MM-dd")+"/"+date_start.toString("HH");
					var widget_advertiser_hour = db.collection('widget_advertiser_hour');
					if(fs.existsSync(path_adv_view)){
						var list_file=fs.readdirSync(path_adv_view);
						for(var i=0;i<list_file.length;i++){
							if(fs.lstatSync(path_adv_view+"/"+list_file[i]).isFile()){
								var content_file=fs.readFileSync(path_adv_view+"/"+list_file[i],"utf8");
								content_file=content_file.split("|");
								/*
								 * content_file_log:link_id | net | widget_code | widget_id | region | city | sms_code | type_widget | date
								 */
								var where={
										link_id:content_file[0],
										net:content_file[1],
										widget_code:content_file[2],
										widget_id:content_file[3],
										region:content_file[4],
										city:content_file[5],
										sms_code:content_file[6],
										type_widget:content_file[7]
								};
								//Find and modify if not exits insert log to table
								widget_advertiser_hour.findAndModify(where, [['link_id', 1]], {$set:{date:new Date(content_file[8])}}, {new:true, upsert:true, w:1}, 
										function(err, doc) {
//										console.log(doc);
											if(typeof doc.views=="undefined"){
												widget_advertiser_hour.findAndModify(where,[['link_id', 1]],{$set:{views:1}},{new:true, upsert:true, w:1},function(err,doc){
//													console.log(doc);
												});
									    	  }else{
									    		  widget_advertiser_hour.findAndModify(where,[['link_id', 1]],{$set:{views:doc.views+1}},{new:true, upsert:true, w:1},function(err,doc){
									    			  console.log(doc);
									    		  });
									    	  }
//										db.close();
								});
								
							}
						}
					}
					
					
					
					//Ghi lai thoi gian cap nhat data
					console.log("UPDATE DATA "+ date_start.toString("yyyy-MM-dd HH mm") + " OK!");
					date_start.addMinutes(STEP_MINUTE);
					fs.writeFileSync(ROOT+"/start.txt",date_start.toString("yyyy-MM-dd HH mm"));
				}
			}else{
	//			console.log("Wait "+date_start.toString("mm"));	
			}
//	}
	});}
	);
}
